package com.mjfeed.controller.admin;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.ReturnMethod;
import com.mjfeed.service.ReturnMethodService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 商品返回方式 
 * @author zhaominglei
 * @date 2014年10月21日
 */
@Controller("adminReturnMethodController")
@RequestMapping("/admin/returnMethod")
public class ReturnMethodController extends BaseController {
	@Resource(name="returnMethodService")
	private ReturnMethodService returnMethodService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/returnMethod/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<ReturnMethod> pagination = returnMethodService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/returnMethod/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/returnMethod/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(ReturnMethod returnMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (returnMethod == null) {
			return ERROR_VIEW;
		}
		returnMethodService.save(returnMethod);
		return "redirect:/admin/returnMethod/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		if (id == null || id < 1) {
			return ERROR_VIEW;
		}
		request.setAttribute("returnMethod", returnMethodService.getById(ReturnMethod.class, id));
		return "admin/returnMethod/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(ReturnMethod returnMethod, HttpServletRequest request, HttpServletResponse response, Model model){
		if (returnMethod == null || returnMethod.getId() == null || returnMethod.getId() < 1) {
			return ERROR_VIEW;
		}
		ReturnMethod pReturnMethod = returnMethodService.getById(ReturnMethod.class, returnMethod.getId());
		pReturnMethod.setName(returnMethod.getName());
		pReturnMethod.setDescription(returnMethod.getDescription());
		pReturnMethod.setOrders(returnMethod.getOrders());
		returnMethodService.update(pReturnMethod);
		return "redirect:/admin/returnMethod/list.shtml";
	}
}
